package common

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/constants"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/common"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/common/option"
	"git.myscrm.cn/golang/stark/v4"
)

type optionServer struct {
	optionService option.OptionServiceIface
}

func NewOptionServer(optionService option.OptionServiceIface) pb.OptionServiceServer {
	return &optionServer{
		optionService: optionService,
	}
}

func (s *optionServer) GetRadarOpptyTypeOption(ctx context.Context, request *pb.GetRadarOpptyTypeOptionRequest) (*pb.GetRadarOpptyTypeOptionResponse, error) {
	resp := &pb.GetRadarOpptyTypeOptionResponse{}

	resp.Options = append(resp.Options, &pb.OpptyTypeOption{
		OpptyType:     constants.OpptyTypeBid,
		OpptyTypeName: constants.GetOpptyTypeName(constants.OpptyTypeBid),
	})
	resp.Options = append(resp.Options, &pb.OpptyTypeOption{
		OpptyType:     constants.OpptyTypeWinBid,
		OpptyTypeName: constants.GetOpptyTypeName(constants.OpptyTypeWinBid),
	})
	resp.Options = append(resp.Options, &pb.OpptyTypeOption{
		OpptyType:     constants.OpptyTypeLand,
		OpptyTypeName: constants.GetOpptyTypeName(constants.OpptyTypeLand),
	})
	resp.Options = append(resp.Options, &pb.OpptyTypeOption{
		OpptyType:     constants.OpptyTypeBuilding,
		OpptyTypeName: constants.GetOpptyTypeName(constants.OpptyTypeBuilding),
	})
	resp.Options = append(resp.Options, &pb.OpptyTypeOption{
		OpptyType:     constants.OpptyTypeSale,
		OpptyTypeName: constants.GetOpptyTypeName(constants.OpptyTypeSale),
	})

	return resp, nil
}

func (s *optionServer) GetRadarAreaOption(ctx context.Context, request *pb.GetRadarAreaOptionRequest) (*pb.GetRadarAreaOptionResponse, error) {
	resp := &pb.GetRadarAreaOptionResponse{}

	res, err := s.optionService.GetRadarAreaOption(ctx)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetRadarAreaOption err:%s", err.Error())
		return nil, err
	}
	if res != nil {
		for _, areaOption := range res.Options {
			resp.Options = append(resp.Options, &pb.AreaOption{
				AreaCode:   areaOption.AreaCode,
				Area:       areaOption.Area,
				ParentCode: areaOption.ParentCode,
			})
		}
	}

	return resp, nil
}

func (s *optionServer) GetOption(ctx context.Context, request *pb.GetOptionRequest) (*pb.GetOptionResponse, error) {
	resp, err := s.optionService.GetOption(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetOption request:%+v, err:%s", request, err.Error())
		return nil, err
	}

	return resp, nil
}

func (s *optionServer) GetInvestCompanyOption(ctx context.Context, request *pb.GetInvestCompanyOptionRequest) (*pb.GetInvestCompanyOptionResponse, error) {
	var errMsg string
	if request.GetOrganizingCode() == "" {
		errMsg = "organizing_code参数不能为空"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}
	resp, err := s.optionService.GetInvestCompanyOption(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetInvestCompanyOption request:%+v, err:%s", request, err.Error())
		return nil, err
	}

	return resp, nil
}
